<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- 通用工具栏组件使用 -->
      <page-tools>
        <!-- 自定义左侧内容 -->
        <template #slot-left>
          <span>共19条记录</span>
        </template>

        <!-- 自定义右侧内容 -->
        <template #slot-right>
          <el-button type="danger" size="small" @click="uploadExcelBtnFn">导入excel</el-button>
          <el-button type="success" size="small">导出excel</el-button>
          <el-button type="primary" size="small" @click="addEmpFromFn">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="employeesList">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" prop="staffPhoto" />
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="工号" prop="workNumber" sortable :sort-method="sortMethodFn" />
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatForFn">
            <template v-slot="scope">
              <span> {{ formatFormOfEmploymentFn(scope.row.formOfEmployment) }} </span>
            </template>
          </el-table-column>
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatForFn" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmployFn(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页区域 -->
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 15, 20, 25]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 新增员工弹框组件 -->
      <el-dialog title="新增员工" :visible.sync="showDialog" @close="closeDialogFn">
        <emp-form ref="empForm" :show-dialog.sync="showDialog" :depart-list="departList" @enterEV="addFn" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getEmployeesListAPI, departmentsListAPI, delEmployeesAPI, addEmployeesAPI } from '@/api'
// 树形结构的转换方法
import { transTree } from '@/utils'
import Employees from '@/api/constant/employees.js'
import EmpForm from '@/views/employees/components/empForm.vue'

export default {
  name: 'Employees',
  components: {
    EmpForm
  },
  data() {
    return {
      query: {
        page: 1, // 页码
        size: 10 // 每页条数
      },
      employeesList: [], // 员工列表
      departList: [], // 部门列表数据
      total: 0, // 数据总条数
      showDialog: false // 添加员工组件的展示
    }
  },
  created() {
    // 调用获取员工列表的方法
    this.getEmployeesListFn()
    this.getDepartmentsListFn()
  },

  methods: {
    // 获取员工列表
    async getEmployeesListFn() {
    // 调用获取员工列表的 API
      const res = await getEmployeesListAPI(this.query)
      console.log(res)
      // 根据返回的状态码给用户提示
      if (!res.success) return this.$message.error(res.message)
      // 将返回的结果赋值 data 中的数据
      this.employeesList = res.data.rows
      this.total = res.data.total
    },
    // 获取树行组件的数据
    async getDepartmentsListFn() {
      const res = await departmentsListAPI()
      this.departList = transTree(res.data.depts, '')
      // if (!res.success) return this.$message.error(res.message)
      // this.treeData = transTree(res.data.depts, '')
      // console.log(this.treeData)
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getEmployeesListFn()
    },
    // 当前页面发生改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getEmployeesListFn()
    },
    // 删除功能
    async delEmployFn(id) {
      // 显示删除询问对话框
      const delRes = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 用户点击了取消，给用户进行提示
      if (delRes === 'cancel') return this.$message.info('取消删除成功')
      const res = await delEmployeesAPI(id)
      console.log(res)
      if (!res.success) return this.$message.error(res.message)
      // 删除成功后的提示
      this.$message.success(res.message)
      // 重新获取数据
      this.getEmployeesListFn()
      // 判断是不是最后一条数据
      if (this.employeesList.length === 1) {
        this.query.page--
        if (this.query.page <= 0) {
          this.query.page = 1
        }
      }
      // 重新获取数据
      this.getEmployeesListFn()
    },
    // 格式化聘用数字转为文字方式1
    formatFormOfEmploymentFn(num) {
      const obj = {
        1: '正式',
        2: '非正式'
      }
      return obj[num]
    },
    // 格式化聘用数字转为文字方式2
    formatForFn(row) {
      const obj = Employees.hireType.find(item => Number(item.id) === Number(row.formOfEmployment))
      return obj ? obj.value : '未知'
    },
    // 实现工号列的排序
    sortMethodFn(a, b) {
      return a.workNumber - b.workNumber // 字符串相减会自动转为数字，返回数字
    },
    addEmpFromFn() {
      this.showDialog = true
    },
    async addFn(data) {
      await addEmployeesAPI(data)
      this.getEmployeesListFn()
    },
    closeDialogFn() {
      this.$refs.empForm.$refs.addForm.resetFields()
    },
    uploadExcelBtnFn() {
      this.$router.push('/excel')
    }
  }
}
</script>

<style lang="scss" scoped></style>
